import _ from 'lodash';
import api from 'utils/api'
function sortBy(arr) { //排序初始化
  return _.sortBy(arr, (item) => {
    return item.sortId
  })
}

function getRoles(callback) { //得到角色列表
  api.get(`/role`).then(res => {
    if (callback) callback(res.data)
    return res.data;
  })
  .catch(err => {
    console.log(err)
  });
  return [];
}

function getUsers(callback) { //得到用户列表
  api.get(`/user`).then(res => {
    let params = {
      id:'',
      name:'全部',
      roleId:''
    }
    res.data.unshift(params);
    if (callback) callback(res.data)
    return res.data;
  })
  .catch(err => {
    console.log(err)
  });
  return [];
}

function getCompany(callback) { //得到企业列表
  api.get(`/company?page=0&size=1000`).then(res => {
    let params = {
      id:'',
      name:'全部'
    }
    res.data.list.unshift(params);
    if (callback) callback(res.data)
    return res.data;
  })
  .catch(err => {
    console.log(err)
  });
  return [];
}

function getGoods(id, callback) { //得到商品列表
  if(!id) return;
  api.get(`/goods?companyId=${id}&page=0&size=1000`).then(res => {
    if (callback) callback(res.data)
    return res.data;
  })
  .catch(err => {
    console.log(err)
  });
  return [];
}

function addAll(arry){ //添加全部
  let param = {
    dKey:'',
    id:'',
    dValue:'全部',
  }
  arry.unshift(param);
  return arry;
}

let departmentList = sortBy(JSON.parse(sessionStorage.departmentList)) //一级科室
let doctorTitleList = sortBy(JSON.parse(sessionStorage.doctorTitleList)) //医生职称
let hosLevelList = sortBy(JSON.parse(sessionStorage.hosLevelList)) //医院等级
let propertyList = sortBy(JSON.parse(sessionStorage.propertyList)) //公司性质
let skillTagList = sortBy(JSON.parse(sessionStorage.skillTagList)) //人员技能标签
let subDepartmentList = sortBy(JSON.parse(sessionStorage.subDepartmentList)) //二级科室
let typeList = sortBy(JSON.parse(sessionStorage.typeList)) //公司类型
let paperTypeList = sortBy(JSON.parse(sessionStorage.paperTypeList)) //证件类型
let hopType = sortBy(JSON.parse(sessionStorage.hopType)) //医院类型
let areaProvince = JSON.parse(sessionStorage.addressProvince) // 省份
typeList = addAll(typeList);
doctorTitleList = addAll(doctorTitleList);
hosLevelList = addAll(hosLevelList);
propertyList = addAll(propertyList);
skillTagList = addAll(skillTagList);
paperTypeList = addAll(paperTypeList);
hopType = addAll(hopType);

let sexList = [ //用户性别
  { id: 1, dKey: 1, dValue: '男' },
  { id: 0, dKey: 0, dValue: '女' },
]
let stateList = [ //用户状态
  { id: 0, dKey: 0, dValue: '启用' },
  { id: -1, dKey: -1, dValue: '停用' },
]
let ipoList = [
  { id: '1234560', dKey: true, dValue: '是' },
  { id: '1234567', dKey: false, dValue: '否' },
]

export {
  departmentList,
  doctorTitleList,
  hosLevelList,
  propertyList,
  areaProvince,
  getRoles,
  getUsers,
  getCompany,
  getGoods,
  skillTagList,
  stateList,
  subDepartmentList,
  typeList,
  ipoList,
  hopType,
  sexList,
  paperTypeList
}
